<?php
/**
*
* @package acp
* @copyright (c) 2009 kmklr72
* @license http://opensource.org/licenses/gpl-license.php GNU Public License 
*
*/

/**
* @ignore
*/
if (!defined('IN_PHPBB'))
{
	exit;
}

/**
* @package acp
*/
class acp_chat
{
	var $u_action;
	var $new_config = array();

	function main($id, $mode)
	{
		global $db, $user, $auth, $template;
		global $config, $phpbb_root_path, $phpbb_admin_path, $phpEx;

		include($phpbb_root_path . 'includes/functions_chat.' . $phpEx);

		$chat = new chat();
		$chat_config = $chat->get_chat_config();
		$this->new_config = $chat_config;

		$action	= request_var('action', '');
		$submit = (isset($_POST['submit'])) ? true : false;

		$form_key = 'acp_chat';
		add_form_key($form_key);

		/**
		*	Validation types are:
		*		string, int, bool,
		*		script_path (absolute path in url - beginning with / and no trailing slash),
		*		rpath (relative), rwpath (realtive, writable), path (relative path, but able to escape the root), wpath (writable)
		*/
		switch ($mode)
		{
			case 'settings':
				$display_vars = array(
					'title'	=> 'ACP_CHAT_SETTINGS',
					'vars'	=> array(
						'legend1'						=> 'ACP_CHAT_SETTINGS',
						'chat_enabled'					=> array('lang' => 'CHAT_ENABLED',							'validate' => 'bool', 'type' => 'radio:yes_no', 'explain' => true),
						'chat_disabled_message'			=> array('lang' => 'CHAT_DISABLED_MESSAGE',					'validate' => 'string', 'type' => 'text:80:200', 'explain' => true),
						'guest_view_enabled'			=> array('lang' => 'CHAT_GUEST_VIEW_ENABLED',				'validate' => 'bool', 'type' => 'radio:yes_no', 'explain' => true),
						'chat_popup_enabled'			=> array('lang' => 'CHAT_POPUP_ENABLED',					'validate' => 'bool', 'type' => 'radio:yes_no', 'explain' => true),
						'update_seconds'				=> array('lang' => 'CHAT_UPDATE_SECONDS',					'validate' => 'int', 'type' => 'text:5:10', 'explain' => true),
						'num_message_display'			=> array('lang' => 'CHAT_NUM_MESSAGE_DISPLAY',				'validate' => 'string', 'type' => 'text:5:10', 'explain' => true),
						'archive_message_limit'			=> array('lang' => 'CHAT_ARCHIVE_MESSAGE_LIMIT',			'validate' => 'int', 'type' => 'text:5:10', 'explain' => true),
						'permissions_enabled'			=> array('lang' => 'CHAT_PERMISSIONS_ENABLED',				'validate' => 'bool', 'type' => 'radio:yes_no', 'explain' => true),
						'channels_enabled'				=> array('lang' => 'CHAT_CHANNELS_ENABLED',					'validate' => 'bool', 'type' => 'radio:yes_no', 'explain' => true),

						'legend2'						=> 'ACP_CHAT_STATUS_SETTINGS',
						'online_time'					=> array('lang' => 'CHAT_ONLINE_TIME',						'validate' => 'int', 'type' => 'text:5:10', 'explain' => true),
						'idle_time'						=> array('lang' => 'CHAT_IDLE_TIME',						'validate' => 'int', 'type' => 'text:5:10', 'explain' => true),
						'offline_time'					=> array('lang' => 'CHAT_OFFLINE_TIME',						'validate' => 'int', 'type' => 'text:5:10', 'explain' => true),

						'legend3'						=> 'ACP_CHAT_STATUS_DELAY_SETTINGS',
						'online_delay'					=> array('lang' => 'CHAT_ONLINE_DELAY',						'validate' => 'int', 'type' => 'text:5:10', 'explain' => true),
						'idle_delay'					=> array('lang' => 'CHAT_IDLE_DELAY',						'validate' => 'int', 'type' => 'text:5:10', 'explain' => true),
						'offline_delay'					=> array('lang' => 'CHAT_OFFLINE_DELAY',					'validate' => 'int', 'type' => 'text:5:10', 'explain' => true),
					)
				);

				$template->assign_var('DISPLAY_VARS', true);

				$this->page_output($display_vars, $form_key);
			break;

			case 'chan_list':
				$delete = request_var('del', 1);
				$cid = request_var('cid', 0);

				$chat->get_channel_list(true);

				if ($delete == 1)
				{
					$chat->del_channel($cid);
				}

				$template->assign_vars(array(
					'L_TITLE'							=> $user->lang['ACP_CHAT_CHANNEL_LIST'],
					'L_TITLE_EXPLAIN'					=> $user->lang['ACP_CHAT_CHANNEL_LIST_EXPLAIN'],

					'CHAT_CHANNEL_LIST'					=> true,
				));
			break;

			case 'chan_create':
				if ($submit)
				{
					$channel = array(
						'channel_name'				=> request_var('channel_name', ''),
						'channel_password'			=> request_var('channel_password', ''),
					);

					$chat->create_channel($channel);
				}

				$template->assign_vars(array(
					'L_TITLE'							=> $user->lang['ACP_CHAT_CHANNEL_CREATE'],
					'L_TITLE_EXPLAIN'					=> $user->lang['ACP_CHAT_CHANNEL_CREATE_EXPLAIN'],

					'CHAT_CHANNEL_CREATE'				=> true,
				));
			break;

			case 'chan_edit':
				/*if ($submit)
				{
				}
				else
				{
				}*/

				$template->assign_vars(array(
					'L_TITLE'							=> $user->lang['ACP_CHAT_CHANNEL_ADM'],
					'L_TITLE_EXPLAIN'					=> $user->lang['ACP_CHAT_CHANNEL_ADM_EXPLAIN'],

					'CHAT_CHANNEL_ADM'					=> true,
				));
			break;

			case 'banned_list':
				$this->list_banned_users();

				$template->assign_vars(array(
					'L_TITLE'							=> $user->lang['ACP_CHAT_BANNED_LIST'],
					'L_TITLE_EXPLAIN'					=> $user->lang['ACP_CHAT_BANNED_LIST_EXPLAIN'],

					'CHAT_BANNED_LIST'					=> true,
				));

				$unban = request_var('unban', '');
				$user_id = request_var('u', 0);

				if ($unban)
				{
					$this->user_unban($user_id);
				}
			break;

			default:
				trigger_error('NO_MODE', E_USER_ERROR);
			break;
		}

		$this->tpl_name = 'acp_chat';
		$this->page_title = (isset($display_vars)) ? $user->lang[$display_vars['title']] : 'Demon Chat';
	}

	function page_output($display_vars, $form_key)
	{
		global $db, $user, $auth, $template;
		global $config, $phpbb_root_path, $phpbb_admin_path, $phpEx;
		//global $chat;

		//include($phpbb_root_path . 'includes/functions_chat.' . $phpEx);

		$action	= request_var('action', '');
		$submit = (isset($_POST['submit'])) ? true : false;

		$chat = new chat();
		//$chat_config = $chat->get_chat_config();
		//$this->new_config = $chat_config;

		$cfg_array = (isset($_REQUEST['config'])) ? utf8_normalize_nfc(request_var('config', array('' => ''), true)) : $this->new_config;
		$error = array();

		// We validate the complete config if whished
		validate_config_vars($display_vars['vars'], $cfg_array, $error);

		if ($submit && !check_form_key($form_key))
		{
			$error[] = $user->lang['FORM_INVALID'];
		}
		// Do not write values if there is an error
		if (sizeof($error))
		{
			$submit = false;
		}

		// We go through the display_vars to make sure no one is trying to set variables he/she is not allowed to...
		foreach ($display_vars['vars'] as $config_name => $null)
		{
			if (!isset($cfg_array[$config_name]) || strpos($config_name, 'legend') !== false)
			{
				continue;
			}

			$this->new_config[$config_name] = $config_value = $cfg_array[$config_name];

			if ($submit)
			{
				$chat->set_chat_config($config_name, $config_value);
			}
		}

		$template->assign_vars(array(
			'L_TITLE'			=> $user->lang[$display_vars['title']],
			'L_TITLE_EXPLAIN'	=> $user->lang[$display_vars['title'] . '_EXPLAIN'],

			'S_ERROR'			=> (sizeof($error)) ? true : false,
			'ERROR_MSG'			=> implode('<br />', $error),

			'U_ACTION'			=> $this->u_action)
		);

		// Output relevant page
		foreach ($display_vars['vars'] as $config_key => $vars)
		{
			if (!is_array($vars) && strpos($config_key, 'legend') === false)
			{
				continue;
			}

			if (strpos($config_key, 'legend') !== false)
			{
				$template->assign_block_vars('options', array(
					'S_LEGEND'		=> true,
					'LEGEND'		=> (isset($user->lang[$vars])) ? $user->lang[$vars] : $vars)
				);

				continue;
			}

			$type = explode(':', $vars['type']);

			$l_explain = '';
			if ($vars['explain'] && isset($vars['lang_explain']))
			{
				$l_explain = (isset($user->lang[$vars['lang_explain']])) ? $user->lang[$vars['lang_explain']] : $vars['lang_explain'];
			}
			else if ($vars['explain'])
			{
				$l_explain = (isset($user->lang[$vars['lang'] . '_EXPLAIN'])) ? $user->lang[$vars['lang'] . '_EXPLAIN'] : '';
			}

			$content = build_cfg_template($type, $config_key, $this->new_config, $config_key, $vars);

			if (empty($content))
			{
				continue;
			}

			$template->assign_block_vars('options', array(
				'KEY'			=> $config_key,
				'TITLE'			=> (isset($user->lang[$vars['lang']])) ? $user->lang[$vars['lang']] : $vars['lang'],
				'S_EXPLAIN'		=> $vars['explain'],
				'TITLE_EXPLAIN'	=> $l_explain,
				'CONTENT'		=> $content,
				)
			);

			unset($display_vars['vars'][$config_key]);
		}

		//return;
	}

	function list_banned_users()
	{
		global $db, $template, $phpbb_root_path, $phpEx;

		$sql = 'SELECT * FROM ' . USERS_TABLE . ' WHERE user_chat_banned = 1';
		$result = $db->sql_query($sql);

		while ($row = $db->sql_fetchrow($result))
		{
			$template->assign_block_vars('banned', array(
				'USER_ID'				=> $row['user_id'],
				'USERNAME'				=> $row['username'],

				'U_USER_CHAT_UNBAN'		=> append_sid("{$phpbb_root_path}adm/index.$phpEx", 'i=chat&amp;mode=banned_list&amp;unban=1&amp;u=' . $row['user_id'], true),
			));
		}
		$db->sql_freeresult($result);

		return;
	}

	function user_unban($user_id)
	{
		global $db;

		$sql = 'UPDATE ' . USERS_TABLE . ' SET user_chat_banned = 0 WHERE user_id = ' . $user_id;
		$db->sql_query($sql);
	}
}

?>